/*
 * Copyright (c) 2009, Research Group on the Foundations of Artificial
 * Intelligence, Department of Computer Science, Albert-Ludwigs University
 * Freiburg. All rights reserved. Redistribution and use in source and binary
 * forms, with or without modification, are permitted provided that the
 * following conditions are met: Redistributions of source code must retain the
 * above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution. The name of the author
 * may not be used to endorse or promote products derived from this software
 * without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE
 * AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// License: GPL. For details, see LICENSE file.
package rcr;

import static org.openstreetmap.josm.tools.I18n.tr;

import java.awt.HeadlessException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.SwingUtilities;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.actions.ExtensionFileFilter;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
import org.openstreetmap.josm.gui.progress.ProgressMonitor;
import org.openstreetmap.josm.io.FileImporter;
import org.openstreetmap.josm.io.IllegalDataException;
import org.openstreetmap.josm.io.OsmReader;

public class RCRImporter extends FileImporter {

  public RCRImporter() {
    super( new ExtensionFileFilter( "osm,xml", "osm",
        tr( "OSM Server Files" ) + " (*.osm *.xml)" ) );
  }


  public RCRImporter( ExtensionFileFilter filter ) {
    super( filter );
  }


  @Override
  public void importData( File file, ProgressMonitor progressMonitor )
      throws IOException, IllegalDataException {
    try {
      FileInputStream in = new FileInputStream( file );
      importData( in, file );
    } catch ( HeadlessException e ) {
      e.printStackTrace();
      throw new IOException( tr( "Could not read \"{0}\"", file.getName() ) );
    } catch ( FileNotFoundException e ) {
      e.printStackTrace();
      throw new IOException( tr( "Could not read \"{0}\"", file.getName() ) );
    }
  }


  protected void importData( InputStream in, File associatedFile )
      throws IllegalDataException {
    DataSet dataSet = OsmReader.parseDataSet( in,
        NullProgressMonitor.INSTANCE );
    RCRDataSet rcrData = new RCRDataSet( dataSet );
    final OsmDataLayer layer = new RCRMapLayer( rcrData,
        associatedFile.getName(), associatedFile );
    Runnable uiStuff = new Runnable() {

      public void run() {
        Main.main.addLayer( layer );
        // layer.fireDataChange();
        layer.onPostLoadFromFile();
      }
    };
    if ( SwingUtilities.isEventDispatchThread() ) {
      uiStuff.run();
    } else {
      SwingUtilities.invokeLater( uiStuff );
    }
  }
}
